<template>
  <div class="employees">
    <el-card class="top">
      <HmTip>
        <template #right>
          <el-button type="primary" round size="mini">导入</el-button>
          <el-button
            type="primary"
            round
            size="mini"
            @click="addClick"
          >+添加员工</el-button>
        </template>
      </HmTip>
    </el-card>
    <el-card class="buttom">
      <el-table :data="list" border>
        <el-table-column label="序号" width="80%">
          <template v-slot="{ $index }">
            <div>
              {{ (page.page - 1) * page.size + $index + 1 }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="姓名" prop="username" />
        <el-table-column label="手机号" sortable prop="mobile" />
        <el-table-column label="工号" sortable prop="workNumber" />
        <el-table-column label="聘用形式" sortable prop="formOfEmployment2">
          <!-- <template v-slot="{ row }">
            <div>
              {{ row.formOfEmployment | formatStr }}
            </div>
          </template> -->
          <!-- 自定义指令 -->
          <!-- <template v-slot="{ row }">
            <div v-directiveForm="row.formOfEmployment">
              {{ row.formOfEmployment }}
            </div>
          </template> -->
        </el-table-column>
        <el-table-column label="部门" sortable prop="departmentName" />
        <el-table-column label="入职时间" sortable prop="timeOfEntry2" />
        <el-table-column label="是否在职" sortable prop="enableState2" />
        <el-table-column label="操作" width="300px" fixed="right">
          <template v-slot="{ row }">
            <div>
              <el-button type="text">查看</el-button>
              <el-button type="text">转正</el-button>
              <el-button type="text">调岗</el-button>
              <el-button type="text">离职</el-button>
              <el-button type="text">角色</el-button>
              <el-button type="text" @click="del(row.id)">删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="page">
        <el-pagination
          :current-page.sync="page.page"
          :page-sizes="[1, 3, 5, 10, 100, 200]"
          :page-size="page.size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="page.total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
    <!-- 弹框 -->
    <!-- <Add :is-show="show" @update:isShow="show = $event" /> -->
    <Add :is-show.sync="show" @getData="getData" />
  </div>
</template>

<script>
import { sysUser, sysUserDelete } from '@/api/employees.js'
import employeesobj from '@/api//constant/employees'
import cookiejs from 'js-cookie'
import Add from './components/add.vue'
import moment from 'moment'
export default {
  components: {
    Add
  },
  // filters: {
  //   formatStr(id) {
  //     const temp = employeesobj.hireType.find((item) => {
  //       return item.id === id
  //     })
  //     // console.log('temp', temp)
  //     return temp ? temp.value : '-'
  //   }
  // },
  data() {
    return {
      list: [{}],
      show: false,
      page: {
        page: 1,
        size: +cookiejs.get('employees') || 1,
        // size: 1,
        total: 100
      }
    }
  },
  created() {
    this.getData()
  },
  methods: {
    // 自带的方法,聘用形式
    formatterForm(row, column, cellValue, index) {
      const _index = employeesobj.hireType.findIndex((item) => {
        return item.id === cellValue
      })
      return _index === -1 ? '-' : employeesobj.hireType[_index].value
    },

    async getData() {
      const res = await sysUser(this.page)
      res.data.rows.forEach((item) => {
        item.formOfEmployment2 = this.formatterForm(
          null,
          null,
          item.formOfEmployment
        )
        item.enableState2 = this.workingStateForm(item.enableState)
        item.timeOfEntry2 = moment(item.timeOfEntry).format('YYYY-MM-DD')
      })

      this.list = res.data.rows
      this.page.total = res.data.total
      console.log(res)
    },
    workingStateForm(id) {
      const resultState = employeesobj.workingState.find((item) => {
        return +item.id === id
      })
      return resultState ? resultState.value : '-'
    },
    // 页码
    handleSizeChange(size) {
      this.page.size = size
      this.page.page = 1
      // 持久化处理
      cookiejs.set('employees', size)
      this.getData()
    },
    // 页容量
    handleCurrentChange(page) {
      this.page.page = page
      this.getData()
    },
    // 新增点击
    addClick() {
      this.show = true
    },
    // 删除
    del(id) {
      this.$confirm('你确定要删除吗?', '温馨提示').then(async() => {
        await sysUserDelete(id)
        this.$message.success('删除成功')
        this.page.page = 1
        this.getData()
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.employees {
  padding: 30px;

  .buttom {
    margin-top: 10px;
  }
  .page {
    margin: 10px 0;
    text-align: right;
  }
  ::v-deep .top .el-card__body {
    padding: 10px;
  }
  ::v-deep .cell {
    text-align: center;
  }
}
</style>
